<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addClick('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 头部的布局结束 -->
        <!-- 主体开始 -->
        <el-tree
          :data="list"
          :props="defaultProps"
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addClick(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length===0" @click.native="delClick(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 准备一个弹窗组件 -->
      <el-dialog title="新增子部门" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false" @close="hClose">
        <!-- 子组件插槽放入里面 -->
        <add-or-edit :id="curId" ref="myDept" :is-edit="isEdit" :origin-list="originList" @close="showDialog=false" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeList } from '@/utils/departments'
import addOrEdit from './depDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      originList: [],
      showDialog: false, // 控制弹窗的显示和隐藏
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      curId: '',
      isEdit: false // false新增 ， true为编辑
    }
  },
  async created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      console.log('关闭了弹窗')
      this.$refs.myDept.resetForm()
    },
    delClick(id) {
      // 删除部门
      this.$confirm('您确定要删除该部门吗？, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        console.log(res)
        if (res.success) {
          this.loadDepartments()
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addClick(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      this.$nextTick(() => {
        this.$refs.myDept.loadDepartmentsDetail()
      })
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    async loadDepartments() {
      try {
        const { data } = await getDepartments()
        console.log(data)
        data.depts.shift()
        this.list = tranListToTreeList(data.depts)
        const originList = data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        this.originList = originList
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
